package com.ityang.hhhh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ityang.hhhh.conm.BaseContext;
import com.ityang.hhhh.conm.R;
import com.ityang.hhhh.domain.BulletChat;
import com.ityang.hhhh.domain.Video;
import com.ityang.hhhh.service.IBulletChatService;
import com.ityang.hhhh.service.IVideoService;
import com.ityang.hhhh.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *  视频
 * @author ityang
 * @since 2022-05-19
 */
@RestController
@RequestMapping("/video")
public class VideoController {
    @Autowired
    private IVideoService videoService;
    @Autowired
    private IBulletChatService bulletChatService;
    /**
     * 查询视频
     * @return
     */
    @GetMapping(("/list"))
    public R<List<Video>> list(){
        try{

            List<Video> videoList = videoService.list();
            return R.success(videoList);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("查询错误");
        }
    }

    /**
     * 发布视频 添加
     * @param video
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody  Video video){
        try{
            videoService.save(video);
        }catch (Exception e){
            e.printStackTrace();
            R.error("添加失败");
        }
        return R.success("添加成功");
    }


    /**
     * 视频文件上传
     * @param videoFile
     * @return
     */
    @PostMapping("/upload")
    public R upload(MultipartFile videoFile){
        String originalFilename = videoFile.getOriginalFilename();//原始文件名
        int index = originalFilename.lastIndexOf(".");//取出"."包含点之后的文件名 .mp4
        String substring = originalFilename.substring(index);//取出"."包含点之后的文件名 .mp4
        String fileName = UUID.randomUUID().toString() + substring;//使用uuid生成文件名

        try {
            //使用七牛云存储视频
            QiniuUtils.upload2Qiniu(videoFile.getBytes(),fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("上传失败");
        }
            return R.success("上传成功");

    }



    /**
     * 根据id查询视频详细信息
     * @param video
     * @return
     */
    @GetMapping("/getId")
    public R getById(@RequestBody Video video){
        try{
            Video service = videoService.getById(video);
            return R.success(service);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("查询失败");
        }
    }

    /**
     * 添加弹幕
     * @param bulletChat
     * @return
     */
    @PostMapping("/saveByChat")
    public R saveByChat(@RequestBody BulletChat bulletChat){
        try {

            bulletChat.setUserId(BaseContext.getCurrentId());
            bulletChatService.save(bulletChat);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("发送失败");
        }
        return R.success("发送成功");
    }

    /**
     * 查询弹幕
     * @return
     */
    @GetMapping("/listByChat")
    public R<Map> listByChat(Long id){
        try{
            LambdaQueryWrapper<BulletChat> lqw = new LambdaQueryWrapper<>();
            lqw.eq(BulletChat::getVideoId,id);
            Map<String, Object> map = bulletChatService.getMap(lqw);
            return R.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}

